package com.md.api.controller;

import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.config.RedisKeys;
import com.md.api.common.utils.RedisUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.*;
import com.md.api.entity.util.OssPage;
import com.md.api.service.UserSevice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.UUID;

@Slf4j
@RequestMapping("/currentUser")
@RestController
public class UserController {

	@Resource
	UserSevice userSevice;
	@Resource
	RedisUtil redisUtil;
//	@NoRepeatSubmit
//	@RequestMapping("/info")
//	public HttpResult getInfoByToken(HttpServletRequest request) {
//
////		String  accessToken = request.getHeader("token");
////		log.info("token:" + accessToken);
////		UserInfo user = (UserInfo)redisUtil.get(RedisKeys.getUserTokenInfoKey(accessToken));
////		return new HttpResult(user);
//
//	}


	@RequestMapping("/info")
	public HttpResult info(HttpServletRequest request) {

		return userSevice.getInfo(request);

	}


	@NoRepeatSubmit
	@RequestMapping("/register")
	public HttpResult register(HttpServletRequest request, @RequestBody UserInfo userInfo) {
		String language = request.getHeader("language");
		if (StringUtils.isEmpty(userInfo.getEmail())){
			if(StringUtils.isNotEmpty(language)&&language.equals("en")){
				return HttpResult.fail("Email cannot be empty");
			}
			return HttpResult.fail("郵箱不能爲空！");
		}
		if (StringUtils.isEmpty(userInfo.getPhone())){
			if(StringUtils.isNotEmpty(language)&&language.equals("en")){
				return HttpResult.fail("Phone cannot be empty");
			}
			return HttpResult.fail("電話不能爲空！");
		}
		if (null==userInfo.getUserType()){
			if(StringUtils.isNotEmpty(language)&&language.equals("en")){
				return HttpResult.fail("User type cannot be empty");
			}
			return HttpResult.fail("用戶類型不能爲空！");
		}
		return userSevice.register(request,userInfo);

	}

	@NoRepeatSubmit
	@RequestMapping("/updateBaseInfo")
	public HttpResult updateBaseInfo(HttpServletRequest request, @RequestBody UserInfo userInfo) {
		return userSevice.updateBaseInfo(request,userInfo);

	}


	@RequestMapping("/login")
	@ResponseBody
	public HttpResult login(HttpServletRequest request, @RequestBody UserInfo user) {

		return userSevice.userLogin(request,user);
	}

	@RequestMapping("/loginOut")
	@ResponseBody
	public HttpResult loginOut(HttpServletRequest request) {

		return userSevice.loginOut(request);

	}

	@RequestMapping("/logOff")
	@ResponseBody
	public HttpResult logOff(HttpServletRequest request) {

		return userSevice.logOff(request);

	}

	@NoRepeatSubmit
	@RequestMapping("/registerFtp")
	public HttpResult registerFtp(HttpServletRequest request, @RequestBody UserFtp ftp) {
		if (StringUtils.isEmpty(ftp.getUserName())||StringUtils.isEmpty(ftp.getPassword())){
			return HttpResult.PARAM_ERROR;
		}
		return userSevice.registerFtp(request,ftp);
	}

	@RequestMapping("/pageOss")
	public HttpResult pageOss(HttpServletRequest request,@RequestBody OssPage page) {
		return userSevice.pageOss(request,page);
	}


	@RequestMapping("/deleteFtp")
	public HttpResult deleteFtp(HttpServletRequest request,@RequestBody PictureInfo pictureInfo) {
		return userSevice.deleteFtp(request,pictureInfo);
	}


}
